package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;
import com.xie.exception.ThisException;
import com.xie.pojo.Salary;
import com.xie.pojo.Tms;
import com.xie.service.TmsService;
import com.xie.util.JwtHelper;
import com.xie.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/Tmss")
public class TmsController {
    @Autowired
    private TmsService tmsService;

//    @RequestMapping(value = "/login")
    @PostMapping("/login")
    @ApiOperation("登陆")
    public Result login(@RequestBody Tms tms){
        // 通过账号和密码查询用户
        String username = tms.getTmsName();
        LambdaQueryWrapper<Tms> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tms::getTmsName,username);
        Tms sysUser = tmsService.getOne(wrapper);

        //3 用户信息是否存在
        if(sysUser == null) {
            throw new ThisException(201,"用户名错误");
        }
        //4 判断密码
        //数据库存密码（MD5）
        String password_db = sysUser.getTmsPswd();
        //获取输入的密码
        String password_input = MD5Util.MD5EncodeUtf8(password_db);
        if(!password_db.equals(password_input)) {
            throw new ThisException(201,"密码错误");
        }
        return Result.ok(tms);
    }

    /**
     * 退出登录
     */
    @PostMapping( "/loginOut")
    @ApiOperation("退出")
    public Result loginOut() {
       return Result.ok();
    }


    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("{pageIndex}/{pageSize}")
    @ApiOperation("分页查询用户信息")
    public Result findAdmin(@PathVariable Integer pageIndex,
                          @PathVariable  Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        IPage<Tms> page1 = tmsService.page(page);
        if (page1 != null) {
            return Result.ok(page1);
        }
        return Result.fail();
    }

    /**
     * 模糊查询管理员
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询管理员信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Tms> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Tms::getTmsDescribe,params)
                .or().like(Tms::getTmsId,params)
                .or().like(Tms::getTmsName,params)
                .or().like(Tms::getTmsPower,params)
                .or().like(Tms::getTmsPswd,params)
                .or().like(Tms::getTmsUser,params)
        ;
        List<Tms> list = tmsService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }
//    /**
//     * 添加管理员信息
//     */
//    @RequestMapping(value = "/addAdmin" ,method = RequestMethod.POST)
//    @ResponseBody
//    public String addAdmin(@RequestBody Tms tms) {
//        tms.setTmsPswd(MD5Util.MD5EncodeUtf8(tms.getTmsPswd()));
//        int a = tmsService.addTms(tms);
//        return "admin_list";
//    }

    /**
     * 添加管理员
     * @param tms
     * @return
     */

    @PostMapping("/save")
    @ApiOperation("添加管理员")
    public Result addSalary(@RequestBody Tms tms) {
        tms.setTmsPswd(MD5Util.MD5EncodeUtf8(tms.getTmsPswd()));
        boolean save = tmsService.save(tms);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 删除管理员信息
     */
//    @RequestMapping( "/deleteAdmin")
//    @ResponseBody
    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除管理员")
    public Result deleteAdmin(@PathVariable Integer id) {
        boolean b = tmsService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }



    /**
     * 导出Excel
     */
//    @RequestMapping(value = "/exportadminlist" , method = RequestMethod.POST)
//    @ResponseBody
    @GetMapping("/list")
    @ApiOperation("获取所有管理员")
    public Result exportAdmin(){
        List<Tms> admin = tmsService.list();
        if (admin != null) {
            return Result.ok(admin);
        }
        return Result.fail();
    }

    /**
     * 根据管理员Id搜索;
     */
//    @RequestMapping( "/findAdminById")
    @GetMapping("/findById/{id}")
    @ApiOperation("根据id获取管理员")
    public Result findAdminById(@PathVariable Integer id) {
        Tms a= tmsService.getById(id);
        if (a != null) {
            return Result.ok(a);
        }
        return Result.fail();
    }

    /**
     * 修改管理员信息
     */
    /**
     * 将提交数据写入Tms对象
     */
//    @RequestMapping( value = "/updateAdmin", method = RequestMethod.POST)
    @PutMapping("/update")
    @ApiOperation("修改管理员信息")
    public Result updateAdmin(@RequestBody Tms tms) {
        tms.setTmsPswd(MD5Util.MD5EncodeUtf8(tms.getTmsPswd()));
        boolean update = tmsService.updateById(tms);
        if (update) {
            return Result.ok();
        }
        return Result.fail();
    }
}